/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bitcar.platform.system.service;

import com.bitcar.common.hibernate.HibernateDao;
import com.bitcar.platform.system.model.Mailconfirm;
import com.bitcar.platform.system.model.User;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author shd
 */
@Service
@Transactional
public class SystemService {
   @Resource
   private HibernateDao dao;

    public HibernateDao getDao() {
        return dao;
    }

    public void setDao(HibernateDao dao) {
        this.dao = dao;
    }
   
    public User getUser(String uname,String pw){
        List ulist= dao.list("from User where username=? and password=?", new String[]{uname,pw});
        if(ulist.size()==1){
            return (User)ulist.get(0);
        }else{
            return null;
        }
    }
    
    public User getUser(String usercode){
        List ulist= dao.list("from User where usercode=?", new String[]{usercode});
        if(ulist.size()==1){
            return (User)ulist.get(0);
        }else{
            return null;
        }
    }
    
    public void saveUser(User u){
        this.dao.save(u);
    }
    
    public void updateUser(User u){
        this.dao.update(u);
    }
    
    public User getUser(int id){
        return (User)this.dao.get(User.class,new Integer(id));
    }
    
    
    public Mailconfirm getMailconfirm(String email,String verifycode,String userid){
       List list= this.dao.list("from Mailconfirm where email=? and verifycode=? and userid=?", 
                new Object[]{email,verifycode,NumberUtils.createInteger(userid)});
       if(list.isEmpty()){
           return null;
       }else{
           return (Mailconfirm)list.get(0);
       }
               
    }
    
    public void saveMailconfirm(Mailconfirm mc){
        this.dao.save(mc);
    }
    
    public void delMailconfirm(String mail,String verifycode,int userid){
        this.dao.uniqueResult("delete from Mailconfirm "
                + " where email=? and verifycode=? and userid=?", new Object[]{mail,verifycode,userid});
        
    }
}
